﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Library;

namespace OA.Exam
{
    public partial class MaintainPaperQuestions : System.Web.UI.Page
    {
        Exam myExam = new Exam();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string paperId = Request["paperid"].ToString();
                string typeId = Request["typeid"].ToString();
                HiddenPaperId.Value = paperId;
                HiddenTypeId.Value = typeId;
                //题型标示
                LabItemType.Text = myExam.GetTypeName(typeId);

                myExam.GetQuestionGroups(DdlGroupLeft);//绑定分组-左
                myExam.GetQuestionGroups(DdlGroupRight);//绑定分组-右

                BindQuestionsLeft();//绑定可选题目列表-左
                BindQuestionsRight(paperId);//绑定已选题目列表-右
            }
        }

        public void BindQuestionsLeft()
        {
            string sql = "select a.QuestionId,Subject from Exam_Questions as a left join Exam_QuestionTypes as b on a.ItemType=b.TypeId left join Exam_QuestionsGroups as c on a.QuestionId=c.QuestionId where a.QuestionId not in(select questionid from Exam_PapersQuestions where paperid='" + HiddenPaperId.Value + "') and a.ItemType='" + HiddenTypeId.Value + "'";
            if (DdlGroupLeft.SelectedValue != "-1")
            {
                sql += " and GroupId='" + DdlGroupLeft.SelectedValue + "'";
            }
            if (TxtSubjectLeft.Text.Trim() != "")
            {
                sql += " and Subject like '%" + TxtSubjectLeft.Text.Trim() + "%'";
            }
            sql += " order by a.QuestionId desc";

            DataTable dt = DataAccess.getDataTable(sql);
            GridViewQuestionsLeft.DataSource = dt.DefaultView;
            GridViewQuestionsLeft.DataBind();
        }

        public void BindQuestionsRight(string paperId)
        {
            string sql = "select a.QuestionId,Subject from Exam_PapersQuestions as a inner join Exam_Questions as b on a.questionid=b.questionid left join Exam_QuestionsGroups as c on a.QuestionId=c.QuestionId where a.paperid='"+paperId+"' and b.ItemType='" + HiddenTypeId.Value + "'";
            if (DdlGroupRight.SelectedValue != "-1")
            {
                sql += " and GroupId='" + DdlGroupRight.SelectedValue + "'";
            }
            if (TxtSubjectRight.Text.Trim() != "")
            {
                sql += " and Subject like '%" + TxtSubjectRight.Text.Trim() + "%'";
            }
            sql += " order by a.QuestionId desc";

            DataTable dt = DataAccess.getDataTable(sql);
            GridViewQuestionsRight.DataSource = dt.DefaultView;
            GridViewQuestionsRight.DataBind();
        }

        protected void BtnSearchLeft_Click(object sender, EventArgs e)
        {
            BindQuestionsLeft();//绑定可选题目列表
        }

        protected void BtnSearchRight_Click(object sender, EventArgs e)
        {
            BindQuestionsRight(HiddenPaperId.Value);//绑定已选题目列表
        }
        //添加题目
        protected void BtnAdd_Click(object sender, EventArgs e)
        {
            int mark = 0;
            string questionId = string.Empty;
            for (int i = 0; i < GridViewQuestionsLeft.Rows.Count; i++)
            {
                CheckBox ChbCheck = (CheckBox)GridViewQuestionsLeft.Rows[i].FindControl("ChbCheck");
                if (ChbCheck.Checked)
                {
                    mark++;
                    questionId = GridViewQuestionsLeft.DataKeys[i].Value.ToString();
                    if (myExam.InsertPaperQuestion(HiddenPaperId.Value, questionId))
                    {
                        LabPrompt.Text = "添加成功";
                    }
                    else
                    {
                        LabPrompt.Text = "添加失败";
                    }
                }
            }
            if (mark == 0)
            {
                LabPrompt.Text = "请选择题目";
                return;
            }
            BindQuestionsLeft();
            BindQuestionsRight(HiddenPaperId.Value);
        }
        //移除题目
        protected void BtnRemove_Click(object sender, EventArgs e)
        {
            int mark = 0;
            string questionId = string.Empty;
            for (int i = 0; i < GridViewQuestionsRight.Rows.Count; i++)
            {
                CheckBox ChbCheck = (CheckBox)GridViewQuestionsRight.Rows[i].FindControl("ChbCheck");
                if (ChbCheck.Checked)
                {
                    mark++;
                    questionId = GridViewQuestionsRight.DataKeys[i].Value.ToString();
                    if (myExam.DeletePaperQuestion(HiddenPaperId.Value, questionId))
                    {
                        LabPrompt.Text = "移除成功";
                    }
                    else
                    {
                        LabPrompt.Text = "移除失败";
                    }
                }
            }
            if (mark == 0)
            {
                LabPrompt.Text = "请选择题目";
                return;
            }
            BindQuestionsLeft();
            BindQuestionsRight(HiddenPaperId.Value);
        }

        protected void BtnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("SetPaperQuestions.aspx?paperId=" + HiddenPaperId.Value);
        }
    }
}
